package cn.stard.org.controller;

import cn.stard.basic.annotation.PreAuthorize;
import cn.stard.basic.util.PageList;
import cn.stard.basic.util.AjaxResult;
import cn.stard.org.domain.SystemtType1;
import cn.stard.org.query.SystemtType1Query;

import cn.stard.org.service.ISystemtType1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*复合注解拥有两个的所有功能*/
@RestController
    @RequestMapping("/system")
public class SystemController {
    @Autowired
    private ISystemtType1Service systemtTypeService;


    @PostMapping
    @PreAuthorize(name="数据字典品牌分页查询所有", sn = "system:list")
    public PageList<SystemtType1> list(@RequestBody SystemtType1Query systemtType1Query ){

        System.out.println("进入查询");
        System.out.println(systemtType1Query);
        PageList<SystemtType1> data = systemtTypeService.queryData(systemtType1Query);

        return data;
    }


    @GetMapping// 请求地址：http://localhost:8080/systemtType1 GET请求
    @PreAuthorize(name="数据字典品牌查询所有", sn = "system:list")
    public List<SystemtType1> loadAll(){
        List<SystemtType1> systemtType1s = systemtTypeService.loadAll();
        return systemtType1s;

    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name="根据id删除数据字典品牌", sn = "system:dele1")
    public AjaxResult dele1(@PathVariable("id")Long id){
        try {
            System.out.println("执行1");
            systemtTypeService.remove(id);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }

    @PatchMapping
    @PreAuthorize(name="批量删除数据字典品牌", sn = "system:deles")
    public AjaxResult deles(@RequestBody List<Long> ids){
        try {

            systemtTypeService.deles(ids);
            return  AjaxResult.me();
        }catch (Exception e){
            System.out.println("执行2");
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }


    }


    @PutMapping
    @PreAuthorize(name="添加修改数据字典品牌", sn = "system:addorupt")
    public AjaxResult addorupt(@RequestBody  SystemtType1 systemtType1){
        try {
            if (systemtType1.getId()==null){
                System.out.println(systemtType1);
                systemtTypeService.save(systemtType1);

            }else {
                systemtTypeService.update(systemtType1);
            }
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }

    }

}
